package com.fast.fastenjoy.base.domain;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * 分页封装类
 * @author chenliaohua
 *
 */
public class Page extends JDomain{

	private static final long serialVersionUID = 1443723899101168893L;

	private static final int DEFAULT_PAGE_SIZE = 10;
	
	private static int DEFAULT_PAGE_START = 1;
	
	public int pageSize = DEFAULT_PAGE_SIZE; 	// 每页的记录数
	public int startIndex = DEFAULT_PAGE_START; // 当前页第一条数据在List中的位置,从1开始
	public List<?> result; 						// 当前页中存放的记录,类型一般为List
	public int totalCount; 						// 总记录数
	public List<Integer> countList;				// 页数list
	public int countPage = 1;
	
	/**
	 * 构造方法，禁止构造空页.
	 */
	private Page() {}

	/**
	 * 默认构造方法.
	 *
	 * @param startIndex	 本页数据在数据库中的起始位置
	 * @param totalSize 数据库中总记录条数
	 * @param pageSize  本页容量
	 * @param data	  本页包含的数据
	 */
	public Page(int start, int totalSize, int pageSize, List<?> result) {
		this.pageSize = pageSize;
		this.startIndex = start;
		this.totalCount = totalSize;
		this.result = result;
		this.countList = getCountList();
	}
	
	public int getCountPage() {
		this.countPage = this.getCountList().size();
		return this.countPage;
	}

	/**
	 * 取总记录数.
	 */
	public int getTotalCount() {
		return this.totalCount;
	}
	
	/**
	 * 设置总记录数.
	 */
	public int setTotalCount(int totalCount) {
		return this.totalCount = totalCount;
	}
	
	/**
	 * 取当前页中的记录.
	 */
	public List<?> getResult() {
		return result;
	}
	
	/**
	 * 设置前页中的记录.
	 */
	public void setResult(List<?> result) {
		this.result = result;
	}
	
	/**
	 * 取总页数.
	 */
	public int getStartIndex() {
		return this.startIndex ;
	}
	
	/**
	 * 取总页数.
	 */
	public int getTotalPageCount() {
		if (totalCount % pageSize == 0)
			return totalCount / pageSize;
		else
			return totalCount / pageSize + 1;
	}

	/**
	 * 取每页数据容量.
	 */
	public int getPageSize() {
		return pageSize;
	}
	
	/**
	 * 设置每页数据条目数
	 */
	public void setPageSize(int apageSize) {
		this.pageSize = apageSize;
	}

	/**
	 * 取该页当前页码,页码从1开始.
	 */
	public int getCurrentPageNo() {
		return startIndex/ pageSize + 1;
	}

	/**
	 * 该页是否有下一页.
	 */
	public boolean hasNextPage() {
		return this.startIndex < this.getTotalPageCount() - 1;
	}

	/**
	 * 该页是否有上一页.
	 */
	public boolean hasPreviousPage() {
		return this.startIndex > 1;
	}

	/**
	 * 获取任一页第一条数据在数据集的位置，每页条数使用默认值.
	 *
	 * @see #getStartOfPage(int,int)
	 */
	protected static int getStartOfPage(int pageNo) {
		return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
	}
	
	public List<Integer> getCountList(){
		
		List<Integer> list = new ArrayList<Integer>();
		
		if(totalCount > 0){
			int _count = 0;
			if(totalCount % pageSize == 0){
				_count = totalCount / pageSize;
			}else{
				_count = totalCount / pageSize + 1;
			}
			for(int i = 1; i <= _count; i++){
				list.add(i);
			}
		}else{
			list.add(1);
		}
		
		return list;
	}
	
	/**
	 * 获取任一页第一条数据在数据集的位置.
	 *
	 * @param pageNo   从1开始的页号
	 * @param pageSize 每页记录条数
	 * @return 该页第一条数据
	 */
	public static int getStartOfPage(int pageNo, int pageSize) {
		return (pageNo - 1) * pageSize;
	}
	
	public static Page getInstance(){
		
		return new Page();
	}
	
	public static Page getInstance(HttpServletRequest request) {
		Page pageObject = new Page();
		if (null == request) return pageObject;
		pageObject.reqProperty(request);
		return pageObject;
	}
	
	public void reqProperty(HttpServletRequest request) {
		if (StringUtils.isNotEmpty(request.getParameter("start"))){
			int startValue = Integer.parseInt(request.getParameter("start"));
			if (startValue >=0){
				this.startIndex =  startValue;
			}
		}
		if (StringUtils.isNotEmpty(request.getParameter("limit"))){
			int limitValue =  Integer.parseInt(request.getParameter("limit"));
			if (limitValue > 0){
				this.pageSize = limitValue;
			}
		}
	}
	
}
